package com.rzjm.api.util.baidu;

import java.net.URLEncoder;
import java.util.*;

public class FaceMatch {
    /**
     * 重要提示代码中所需工具类
     * FileUtil,Base64Util,HttpUtil,GsonUtils请从
     * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
     * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
     * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
     * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
     * 下载
     */
    public static String faceMatch() {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/match";
        System.out.println("开始：" + new Date());
        try {
            List<Map<String,Object>> list = new ArrayList<>();
            Map<String,Object> map = new HashMap<>();
            byte[] imgData = FileUtil.readFileByBytes("D:\\personal\\1.jpg");
            String imgStr = Base64Util.encode(imgData);
            map.put("image",imgStr);
            map.put("image_type","BASE64");
            map.put("face_type","LIVE");
            map.put("quality_control","LOW");
            map.put("liveness_control","NORMAL");

            Map<String,Object> map1 = new HashMap<>();

            byte[] imgData1 = FileUtil.readFileByBytes("D:\\personal\\2.jpg");
            String imgStr1 = Base64Util.encode(imgData1);
            map1.put("image",imgStr1);
            map1.put("image_type","BASE64");
            map1.put("face_type","LIVE");
            map1.put("quality_control","LOW");
            map1.put("liveness_control","NORMAL");
            list.add(map);
            list.add(map1);
            String param = GsonUtils.toJson(list);

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = AuthService.getAuth();

            String result = HttpUtil.post(url, accessToken, "application/json", param);
            System.out.println(result);
            System.out.println("结束：" + new Date());
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        FaceMatch.faceMatch();
    }
}
